package com.jgsu.farm.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: farm-10-14-2019
 * @description: 登录拦截器
 * @author: Mr.zuo
 * @create: 2019-12-02 12:45
 **/

public class LoginInterceptor implements HandlerInterceptor {
    private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("LoginInterceptor==========>在请求处理之前进行调用（Controller方法调用之前）");
        String phone = (String) request.getSession().getAttribute("phone");
        logger.info("当前请求的路径为{}，管理员的phone为{}",request.getRequestURI(),phone);
        if ("".equals(phone)||phone==null){
            logger.info("请先登录...");
            return false;
        }else {
            logger.info("已登录...");
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.info("postHandle.....");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.info("afterCompletion.....");
    }
}
